package com.yan.qing.controller;

import com.yan.qing.util.MinIoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.UUID;

/**
 * @description: 测试
 * @author: lf
 * @create: 2022年05月21日 23:19:00
 */
@RestController
@RequestMapping ("/minio")
public class TestController {

    @Autowired
    private MinIoUtil minIoUtil;


    @GetMapping ("/hello")
    public String hello() {
        return "hello";
    }

    @PostMapping ("/fileUpload")
    public String fileUpload(
            MultipartFile file) {
        // 1.获取文件名
        String originalFilename = file.getOriginalFilename();

        // 2.修改文件名,防止上传重复文件名,导致文件覆盖
        String fileName = UUID.randomUUID().toString().replace("-", "") + "_" + originalFilename;
        // 文件流
        try ( InputStream inputStream = file.getInputStream()) {
            String yan = minIoUtil.upload (file, "yan");

            // 9.访问路径
            return yan;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/down")
    public void download(HttpServletResponse response, String buckName,String fileName){
        minIoUtil.download (buckName,fileName,response);
    }
}